﻿$(function () {
    warehouseShow()
});
function warehouseShow() {
    layui.use(function () {
        var table = layui.table;
        var WarehouseNumber = $("#warehouseNumber").val();
        var WpositionNumber = $("#positionNumber").val();
        var WarehouseState = $("#warehouseState").val();
        console.log(table)
        // 先进行表格的初始渲染
        table.render({
            elem: '#carriertable',  // 表格元素的选择器
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            toolbar: '#toolbarDemo',
            defaultToolbar:['exports'],
            cols: [[
                { type: 'checkbox', fixed: 'left', title: '复选框' },
                { field: 'warehouseNumber', title: '仓库编号' },
                { field: 'warehouseName', title: '仓库名称' },
                { field: 'selectWarehouseArea', title: '仓库区域' },
                { field: 'positionNumber', title: '位置编码' },
                { field: 'warehouseArea', title: '面积' },
                {
                     field: 'warehouseState', title: '状态', templet: function (d) {
                        return d.warehouseState==1?"正常":"异常";
                    }
                },
                { field: 'principal', title: '负责人' },
                {
                    field: '', title: '平面图', templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="Examine()"><i class="layui-icon layui-icon-edit"></i>查看</a> ';
                    }
                },
                { field: 'warehouseRemark', title: '备注' },
                {
                    field: '', title: '操作', templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="Compile(' + d.warehouseID + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="Delete(' + d.warehouseID + ')"><i class="layui-icon layui-icon-Delete"></i>删除</a>' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="Particulars(' + d.warehouseID + ')"><i class="layui-icon layui-icon-edit"></i>详情</a>';
                    }
                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });
        // 创建表格实例
        $.ajax({
            url: "/Warehouses/LedgerInfowarehouseShow",
            type: "get",
            data: {
                pageindex: 1,
                pagesize: 100,
                warehouseNumber: WarehouseNumber,
                positionNumber: WpositionNumber,
                warehouseState: WarehouseState||0
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.dtoList);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('carriertable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.plist
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });
    });
}
//跳转添加
function warehouseADD() {
    location.href = "/Warehouses/warehouseADD";
}
//跳转平面图
function Examine() {
    location.href = "/Warehouses/PrincipalShow";
}
//删除
window.Delete = function (id) {
    console.log(id);
    layer.confirm('确定要删除该条数据吗？', function (index) {
        $.ajax({
            type: "Post",
            url: "/Warehouses/LedgerInfowarehouseDelete",  // 替换为实际的后台删除数据接口地址
            data: { data: JSON.stringify({ warehouseID: id }), token: localStorage.getItem("token") },
            success: function (res) {
                var res = JSON.parse(res)
                if (res.code >= 1) {
                    layer.alert("删除成功");
                    warehouseShow();  // 重新加载数据，刷新表格显示
                } else {
                    layer.alert("删除失败");

                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }
        });
        layer.close(index);
    });



};
//跳转修改
function Compile(warehouseID) {
    localStorage["warehouseID"] = warehouseID
    location.href = "/Warehouses/warehouseUpdate";
}
//跳转详情
function Particulars(warehouseID) {
    localStorage["warehouseID"] = warehouseID
    location.href = "/Warehouses/ParticularsShowWarehouse";
}



